﻿namespace MvcApplication.service
{
    using MvcEntity;
    using System;
    using System.Linq;
    using System.Collections.Generic;
    using MvcApplication.utls;
    using System.Data.Entity;

    public  class DemoService
    {

        public ResponseReslut listOrder()
        {
            using (var db = new DemoDbContext())
            {
                return db.OrderInfoTest.ToList().CreateSuccess();
            }
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="account">账号.</param>
        /// <param name="password">密码.</param>
        /// <returns></returns>
        public ResponseReslut LoginForPwd(string account,string password) {

            DateTime loginNow = DateTime.Now;
            var reslt = new ResponseReslut();
            using (var db = new DemoDbContext())
            {
                DemoUser demoUser = db.DemoUser.Where(x => x.UserName == account).FirstOrDefault();
                if (string.IsNullOrWhiteSpace(demoUser?.Id))
                {
                    reslt.code = 400;
                    reslt.msg = "用户名或密码不正确！";
                    return reslt;
                }
                if (password.entryMd5()!=demoUser.UserPassword)
                {
                    reslt.code = 400;
                    reslt.msg = "用户名或密码不正确！";
                    return reslt;
                }
                demoUser.LastDate = loginNow;
                demoUser.LoginAt += 1;
                // 设置EF对象的更新状态
                db.Entry(demoUser).State = EntityState.Modified;
                // 提交到数据库
                db.SaveChanges();
                reslt.code = 200;
                reslt.msg = "ok";
                demoUser.UserPassword = string.Empty;
                reslt.data = demoUser;
                return reslt;
            }

        }

        public ResponseReslut deleteUser(string id,string currid)
        {
            var reslt = new ResponseReslut();
            using (var db = new DemoDbContext())
            {
               var dbDemoUser = db.DemoUser.Where(x => x.Id == id).FirstOrDefault();
                if (!string.IsNullOrWhiteSpace(dbDemoUser?.Id))
                {
                    if (id?.Trim() == currid?.Trim())
                    {
                        reslt.code = 400;
                        reslt.msg = "无法删除登录的账户。";
                        return reslt;
                    }
                    if (dbDemoUser.UserName == "admin")
                    {
                        reslt.code = 400;
                        reslt.msg = "管理员账户无法删除。";
                        return reslt;
                    }
                    db.DemoUser.Remove(dbDemoUser);
                    db.SaveChanges();
                }
            }
            reslt.code = 200;
            reslt.msg = "ok";
            return reslt;
        }


        public List<DemoUser> GetList()
        {
            using (var db = new DemoDbContext())
            {
                List<DemoUser> demoUsers = db.DemoUser.Where(x => x.UserName == "cdy").ToList();
                return demoUsers;
            }
        }


        public List<DemoUser> GetList(string userName, string phone,out int total, int pageSize=10,int pageNum =1)
        {
            using (var db = new DemoDbContext())
            {
                var templist = db.DemoUser.AsQueryable();
                if (!string.IsNullOrWhiteSpace(userName))
                {
                    templist = templist.Where(x => x.UserName.Contains(userName));
                }
                if (!string.IsNullOrWhiteSpace(phone))
                {
                    templist = templist.Where(x => x.UserPhone.Contains(phone));
                }
                total = templist.Count();
                return templist.OrderByDescending(p => p.CreateDate).Skip((pageNum-1) * pageSize).Take(pageSize).ToList();
            }
        }   

        public DemoUser GetListSign()
        {
            using (var db = new DemoDbContext())
            {
                DemoUser demoUsers = db.DemoUser.Where(x => x.UserName == "cdy").FirstOrDefault();
                return demoUsers;
            }
        }

        /// <summary>
        /// 新增demouser.
        /// </summary>
        public ResponseReslut addDemo(DemoUser request)
        {
            var reslt = new ResponseReslut();
            using (var db = new DemoDbContext())
            {
                DateTime dt = DateTime.Now;
                //判断用户名称不能重复
                DemoUser demoUsers = db.DemoUser.Where(x => x.UserName == request.UserName).FirstOrDefault();
                bool vilidatePhone = db.DemoUser.Where(x => x.UserPhone == request.UserPhone).Count()>0;
                if (vilidatePhone)
                {
                    reslt.code = 400;
                    reslt.msg = "注册失败，手机号重复。";
                    return reslt;
                }
                if (string.IsNullOrWhiteSpace(demoUsers?.Id))
                {
                    request.CreateDate = dt;
                    request.LastDate = dt;
                    request.LoginAt = 0;
                    request.UserPassword = request.UserPassword?.entryMd5();
                    request.Id = Guid.NewGuid().ToString();
                    // 提交到内存中
                    db.DemoUser.Add(request);
                    // 提交到数据库
                    db.SaveChanges();
                    reslt.code = 200;
                    reslt.msg = "ok";
                   return reslt;
                }
                else {
                    reslt.code = 400;
                    reslt.msg = "注册失败，用户账户名称已重复。";
                    return reslt;
                }

            }
        }


        /// <summary>
        /// 修改密码。
        /// </summary>
        /// <param name="id">唯一标识</param>
        /// <param name="oldPwd">旧密码.</param>
        /// <param name="newPwd">新密码.</param>
        /// <returns>返回ResponseReslut</returns>
        public ResponseReslut ModifyPwd(string id,string oldPwd,string newPwd) {
            string errorMsg = string.Empty;
            var reslt = new ResponseReslut();
            using (var db = new DemoDbContext())
            {
                //1.通过Id去查询用户
                var demoUser = db.DemoUser.Where(x => x.Id == id).FirstOrDefault();
                if (!string.IsNullOrWhiteSpace(demoUser?.Id))
                {
                    string requestMd5pwd = oldPwd.entryMd5();
                    string dbpwd = demoUser.UserPassword;
                    //2.md5加密处理oldpwd和数据库的比较
                    if (requestMd5pwd == dbpwd)
                    {
                        //3.如果通过的就更新密码
                        demoUser.UserPassword = newPwd.entryMd5();
                        db.Entry(demoUser).State = EntityState.Modified;
                        // 提交到数据库
                        db.SaveChanges();
                        return CreateResponseReslut.CreateSuccess();
                    }
                    //4.如果是没有通过咱们就返回一个错误。
                    else
                    {
                        errorMsg = "用户信息不存";
                        return errorMsg.CreateFail();
                    }
                }
                else
                {
                    errorMsg = "用户信息不存";
                    return errorMsg.CreateFail();
                }
            }
        }


        public ResponseReslut ModifyPhone(string id, string phoneNumber)
        {
            string errorMsg = string.Empty;
            using (var db = new DemoDbContext())
            {
                var demoUser = db.DemoUser.Where(x => x.Id == id).FirstOrDefault();
                if (!string.IsNullOrWhiteSpace(demoUser?.Id))
                {
                    demoUser.UserPhone = phoneNumber;
                    db.Entry(demoUser).State = EntityState.Modified;
                    // 提交到数据库
                    db.SaveChanges();
                    return CreateResponseReslut.CreateSuccess();
                }
                else {
                    errorMsg = "修改失败";
                    return errorMsg.CreateFail();
                }
            }
        }


    }
}
 